package base.IntegratedPlanning;

import lombok.extern.slf4j.Slf4j;
import util.Sleeper;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;

@Slf4j(topic = "c.IntegratedPlanning ")
public class TestIntegratedPlanning {
    static AtomicBoolean flag = new AtomicBoolean(false);
    public static void main(String[] args) {
        CountDownLatch latch = new CountDownLatch(2);
        Thread t1 = new Thread(() -> {
            log.debug("洗水壶");
            Sleeper.sleep(1);
            log.debug("烧开水");
            Sleeper.sleep(5);
            latch.countDown();

            try {
                latch.await();
                while (flag.compareAndSet(false,true)){
                    log.debug("泡茶");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }, "worker1");

        Thread t2 = new Thread(()->{
            log.debug("洗茶壶");
            Sleeper.sleep(1);
            log.debug("洗茶叶");
            Sleeper.sleep(2);
            log.debug("拿茶叶");
            Sleeper.sleep(1);
            latch.countDown();
            try {
                latch.await();
                while (flag.compareAndSet(false,true)){
                    log.debug("泡茶");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"worker2");

        t1.start();
        t2.start();

    }
}
